package com.wx.controller;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wx.common.JsonObjectResult;
import com.wx.common.redis.RedisCacheHand;
import com.wx.common.redis.RedisUtil;
import com.wx.entity.GoodsPrice;
import com.wx.entity.order.CreateResult;
import com.wx.entity.order.GoodsJsonStr;

@RestController
@RequestMapping("/api")
public class OrderController {

	@Autowired
	private RedisUtil redis;
	
	@PostMapping("/order/create")
	@ResponseBody
	public JsonObjectResult wxapp_formId(@RequestParam(value = "token") String token,
			@RequestParam(value = "goodsJsonStr") String goodsJsonStr,
			@RequestParam(value = "remark") String remark,
			@RequestParam(value = "peisongType") String peisongType,
			@RequestParam(value = "calculate", required = false) String calculate) {
		List<GoodsJsonStr> goodsList = JSON.parseArray(goodsJsonStr, GoodsJsonStr.class);
		GoodsPrice price = new GoodsPrice();
		BigDecimal amountTotle = new BigDecimal(0);
		Integer goodsNumber = 0;
		
		if(goodsList.size() > 0) {
			if("true".equals(calculate)) {
				CreateResult result = new CreateResult();
				for(GoodsJsonStr item : goodsList) {
					price = JSON.parseObject(redis.get(RedisCacheHand.GoodsPriceCache + item.getGoodsId() + ":" + item.getPropertyChildIds()), GoodsPrice.class);
					amountTotle = price.getPrice().add(amountTotle);
					goodsNumber += item.getNumber();
				}
				result.setAmountLogistics(0);
				result.setAmountTotle(amountTotle.doubleValue());
				result.setFree_shipping_for_purchases(1000);
				result.setGoodsNumber(goodsNumber);
				result.setNeedLogistics(false);
				result.setScore(0);
				
				return new JsonObjectResult(JSONObject.parseObject(JSON.toJSONString(result)));
			}
		}
		
		return new JsonObjectResult(0, null, "success");
	}
	
}
